package 代码随想录.贪心;

public class a53最大子数组和 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {5, 4, -1, 7, 8};
        System.out.println(solution.maxSubArray(nums));
    }

    static class Solution {
        public int maxSubArray(int[] nums) {
            int ans = Integer.MIN_VALUE;
            int len = nums.length;
            int sum = 0;
            for (int i = 0; i < len; i++) {
                if (sum <= 0) {
                    sum = nums[i];
                } else {
                    sum += nums[i];
                }
                if (sum > ans) {
                    ans = sum;
                }
            }
            return ans;
        }
    }
}
